from flask import Flask
from flask_cors import CORS
from flask_socketio import SocketIO
# 统一在此处导入
from APP.database import db
from APP.routes.cpu import cpu_bp
from APP.models import *  # 确保模型被加载
from APP.routes.test import api
from flask_socketio import SocketIO
from APP.service.ai_conversation import ai_conversation
socketio = SocketIO(cors_allowed_origins="*", async_mode='threading')# 新增导入
from InfluxDB_setting.service.select_dangerouslist import dangerouslist
from InfluxDB_setting.service.generate_combined_report import generate  # 确保模块被加载
from APP.service.show_test import danger_charts_bp
def create_app():
    app = Flask(__name__)
    # 数据库配置
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:1234@localhost/kylin_2025'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.config['SQLALCHEMY_POOL_SIZE'] = 5
    app.config['SQLALCHEMY_POOL_RECYCLE'] = 280

    # 确保正确初始化db
    db.init_app(app)
    CORS(app)
    # 初始化socketio
    socketio.init_app(app)
    app.register_blueprint(cpu_bp)
    app.register_blueprint(api)
    app.register_blueprint(ai_conversation)
    # 注册危险图表路由蓝图
    app.register_blueprint(danger_charts_bp)

    # 注册未来测试路由蓝图（使用唯一名称）
    from APP.service.future_test import danger_future
    app.register_blueprint(danger_future, name='danger_future')

    return app
